// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//

#include <kllm/openai/model.h>
#include <turbo/log/logging.h>

namespace kllm {

    void format_aoi_model_list_response(const KaiResponse &response, nlohmann::ordered_json &obj, std::string &errmsg,
                                        int &code) {
        CHECK(response.query_type() == QUERY_MODEL_LIST)
                        << "bad query type for this function, it should be QUERY_MODEL_LIST";
        if (response.status().has_errmsg()) {
            errmsg = response.status().errmsg();
        }
        code = response.status().code();

        nlohmann::ordered_json models;
        models["object"] = "list";
        models["data"] = nlohmann::ordered_json::array();
        for (auto &it: response.model_response().models()) {
            nlohmann::ordered_json json;
            json["id"] = it.id();
            json["object"] = it.object();
            json["created"] = it.created();
            json["owned_by"] = it.owned_by();

            nlohmann::ordered_json neta_json;
            neta_json["vocab_type"] = it.meta().vocab_type();
            neta_json["n_vocab"] = it.meta().n_vocab();
            neta_json["n_ctx_train"] = it.meta().n_ctx_train();
            neta_json["n_embd"] = it.meta().n_embd();
            neta_json["n_params"] = it.meta().n_params();
            neta_json["size"] = it.meta().size();

            json["meta"] = std::move(neta_json);

            models["data"].push_back(std::move(json));
        }
        obj = std::move(models);
    }
}  // namespace kllm
